pipeline {
    agent {
        node('built-in')
    }
    parameters {
        string(name: 'namespace', defaultValue: 'dev',
                description: 'Input the namespace to deploy')
    }
    stages {
        stage('Deployment') {
            steps {
                sh """
                configPath=/home/appl/jenkins_slave/.kube
                imageVer=latest
                kubectl apply -f ${params.namespace}/configmap.yaml -n tscqueuing-${params.namespace}  --kubeconfig=\$configPath/config.${params.k8sServer}
                if [ `kubectl get secret -n tscqueuing-${params.namespace} --kubeconfig=\$configPath/config.${params.k8sServer} | grep tscqueuing-secret | wc -l` -gt 0 ]; then
                 kubectl delete secret tscqueuing-secret -n tscqueuing-${params.namespace} --kubeconfig=\$configPath/config.${params.k8sServer}
                fi
                kubectl apply -f ${params.namespace}/secretMap.yml -n tscqueuing-${params.namespace} --kubeconfig=\$configPath/config.${params.k8sServer}

                cd \$WORKSPACE/k8s

                kubectl apply -f tscqueuing-backend-service.yml -n tscqueuing-${params.namespace} --record --kubeconfig=\$configPath/config.${params.k8sServer}

                kubectl apply -f tscqueuing-backend-deployment.yml -n tscqueuing-${params.namespace} --record --kubeconfig=\$configPath/config.${params.k8sServer}

                if [ ${params.pic} = false ]; then
                    kubectl apply -f ${params.namespace}/tscqueuing-backend-ingress.yaml  -n tscqueuing-${params.namespace}  --kubeconfig=\$configPath/config.${params.k8sServer}
                fi
   
                if [ ${params.pic} = true ]; then
                    kubectl apply -f ${params.namespace}/tscqueuing-backend-ingress-pic.yaml -n tscqueuing-${params.namespace} --record --kubeconfig=\$configPath/config.${params.k8sServer}
                fi
           """
            }
        }
    }
}
